package com.fancon.android.hotgirlcollection;

import java.util.ArrayList;

import net.arnx.jsonic.JSON;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.fancon.android.ui.widget.ImageCacheView;
import com.fancon.android.utils.DeviceUtils;
import com.fancon.android.xchannel.entity.Photo;

public class XChannelAdapter extends BaseAdapter implements Filterable {
	private LayoutInflater mInflater;
	private Context context;
	private ArrayList<Photo> lisPhoto;
	private String jsonList;

	public XChannelAdapter(Context context, ArrayList<Photo> lisPhoto) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.lisPhoto = lisPhoto;
		jsonList = JSON.encode(lisPhoto);
	}

	public ArrayList<Photo> getLisPhoto() {
		return lisPhoto;
	}

	public void setLisPhoto(ArrayList<Photo> lisPhoto) {
		this.lisPhoto = lisPhoto;
		jsonList = JSON.encode(lisPhoto);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int ret = lisPhoto.size() % 3 == 0 ? lisPhoto.size() / 3 : lisPhoto
				.size() / 3 + 1;
		return ret;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.img_item, null);
			holder = new ViewHolder();
			holder.thumb = (ImageCacheView) convertView
					.findViewById(R.id.img_thumb);
			holder.thumb1 = (ImageCacheView) convertView
					.findViewById(R.id.img_thumb1);
			holder.thumb2 = (ImageCacheView) convertView
			.findViewById(R.id.img_thumb2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			final int thumbIdx = arg0 * 3;
			final int thumb1Idx = arg0 * 3 + 1;
			final int thumb2Idx = arg0 * 3 + 2;
			final String imgUrl = lisPhoto.get(thumbIdx).getImageUrl();
			final String imgUrl1 = lisPhoto.get(thumb1Idx).getImageUrl();
			final String imgUrl2 = lisPhoto.get(thumb2Idx).getImageUrl();
			String xthumb = getThumbImage(imgUrl);
			String xthumb1 = getThumbImage(imgUrl1);
			String xthumb2 = getThumbImage(imgUrl2);

			holder.thumb.setLoadingImg(R.drawable.loading);
			holder.thumb.setErrorImg(R.drawable.noimage);
			holder.thumb1.setLoadingImg(R.drawable.loading);
			holder.thumb1.setErrorImg(R.drawable.noimage);
			holder.thumb2.setLoadingImg(R.drawable.loading);
			holder.thumb2.setErrorImg(R.drawable.noimage);
			if (!DeviceUtils.isTablet(context)) {
				holder.thumb.loadImage(xthumb, true);
				holder.thumb1.loadImage(xthumb1, true);
				holder.thumb2.loadImage(xthumb2, true);
			} else {
				holder.thumb.loadImage(xthumb);
				holder.thumb1.loadImage(xthumb1);
				holder.thumb2.loadImage(xthumb2);
			}
			holder.thumb.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, PhotoDetailActivity.class);
					i.putExtra("photoUrl", imgUrl);
					i.putExtra("index", thumbIdx);
					i.putExtra("listPhoto", jsonList);
					context.startActivity(i);
				}
			});
			holder.thumb1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, PhotoDetailActivity.class);
					i.putExtra("photoUrl", imgUrl1);
					i.putExtra("index", thumb1Idx);
					i.putExtra("listPhoto", jsonList);
					context.startActivity(i);
				}
			});
			holder.thumb2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, PhotoDetailActivity.class);
					i.putExtra("photoUrl", imgUrl2);
					i.putExtra("index", thumb2Idx);
					i.putExtra("listPhoto", jsonList);
					context.startActivity(i);
				}
			});

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return convertView;
	}

	private String getThumbImage(String url) {
		int index = url.lastIndexOf(".");
		String suf = url.substring(index + 1);
		String pre = url.substring(0, index);
		return pre + "m." + suf;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	static class ViewHolder {
		ImageCacheView thumb;
		ImageCacheView thumb1;
		ImageCacheView thumb2;
	}
}
